package com.itheima.controller;

import com.itheima.annotation.Log;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RequestMapping("/emps")
@RestController
public class EmpController {
    @Autowired
    private EmpService empService;

    @GetMapping
    public Result page(String name, Integer gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("条件分页查询，姓名：{}, 性别：{}, 入职日期：{} - {}", name, gender, begin, end);
//        PageBean pageBean = empService.list(page, pageSize);
        // 调用PageHelper的分页查询功能
        PageBean pageBean = empService.page(name, gender, begin, end, page, pageSize);

        // 响应数据
        return Result.success(pageBean);
    }

    @Log
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        log.info("删除部门，部门id：{}", ids);
        empService.delete(ids);

        // 响应数据
        return Result.success();
    }

    @Log
    @PostMapping
    public Result save(@RequestBody Emp emp) {
        log.info("新增员工，员工信息：{}", emp);

        empService.save(emp);

        // 响应数据
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据id查询员工，员工id：{}", id);

        Emp emp = empService.getById(id);

        // 响应数据
        return Result.success(emp);
    }

    @Log
    @PutMapping
    public Result update(@RequestBody Emp emp) {
        log.info("修改员工，要修改的信息：{}", emp);

        empService.update(emp);

        // 响应数据
        return Result.success();
    }
}
